----------------------------------------------------------------------------------------------------------------------------------------------------
--[[

]]--
----------------------------------------------------------------------------------------------------------------------------------------------------
---
    local assets =
    {
        Asset("ANIM", "anim/hutao_equipment_black_hoodie_of_a_depressed_girl.zip"),
    }
----------------------------------------------------------------------------------------------------------------------------------------------------
-- 参数
    local MAX_FINITE_USES = 5*480           --- 最大使用次数
    local SHELL_START_TIME = 10             --- 护盾开始时间
    local HUNGER_COST_PECENT = 0.75         --- 消耗饥饿百分比
    local WHIP_ATTACK_RADIUS = 7            --- 鞭子攻击范围
----------------------------------------------------------------------------------------------------------------------------------------------------
---激活【郁愈域】
    ---乾坤帽里的额外装备数量
    local function GetExtraEquipCount(inst, owner)
        local hat = owner.components.inventory:GetEquippedItem(EQUIPSLOTS.HEAD)
        if not (hat and hat.prefab == "hutao_equipment_cosmic_hat_sky") then
            return 0
        end
        if hat.components.container == nil then
            return 0
        end
        local ret = 0
        hat.components.container:ForEachItem(function(item)
            if item and item.prefab == inst.prefab then
                ret = ret + 1
            end
        end)
        return ret
    end
    local function activate_healing_grove(inst, owner)
        local current_hunger = owner.replica.hunger:GetCurrent()
        local cost_hunger = current_hunger * HUNGER_COST_PECENT
        owner.components.hutao_com_healing_grove:DoDelta( cost_hunger*(2+GetExtraEquipCount(inst, owner) ))
        owner.components.hunger:DoDelta(-cost_hunger)
        owner.components.hutao_com_dialogue:Say("healing_grove.created")
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 鞭子事件
    local CRACK_MUST_TAGS = { "_combat" }
    local CRACK_CANT_TAGS = { "player", "epic", "shadow", "shadowminion", "shadowchesspiece", "INLIMBO", "whip_crack_imune" }
    local function supercrack(inst)
        local actived_flag = false
        local owner = inst.components.inventoryitem and inst.components.inventoryitem:GetGrandOwner() or nil
        local x,y,z = inst.Transform:GetWorldPosition()
        local ents = TheSim:FindEntities(x,y,z, WHIP_ATTACK_RADIUS, CRACK_MUST_TAGS, CRACK_CANT_TAGS)
        for i,v in ipairs(ents) do
            if v ~= owner and v.components.combat:HasTarget() then
                v.components.combat:DropTarget()
                actived_flag = true
                if v.sg ~= nil and v.sg:HasState("hit")
                    and v.components.health ~= nil and not v.components.health:IsDead()
                    and not v.sg:HasStateTag("transform")
                    and not v.sg:HasStateTag("nointerrupt")
                    and not v.sg:HasStateTag("frozen")
                    --and not v.sg:HasStateTag("attack")
                    --and not v.sg:HasStateTag("busy")
                    then

                    if v.components.sleeper ~= nil then
                        v.components.sleeper:WakeUp()
                    end
                    v.sg:GoToState("hit")
                end
            end
        end
        return actived_flag
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 玩家吃东西
    local mushroom_prefab = {
        ["hutao_cooked_food_vegetarian_abalone"] = true,        -- 素鲍鱼
        ["hutao_cooked_food_ghostly_march"] = true,             -- 幽幽大行军
        ["red_cap"] = true,                                     -- 红伞
        ["red_cap_cooked"] = true,                              -- 红伞
        ["green_cap"] = true,                                   -- 绿伞
        ["green_cap_cooked"] = true,                            -- 绿伞
        ["blue_cap"] = true,                                    -- 蓝伞
        ["blue_cap_cooked"] = true,                             -- 蓝伞
        ["moon_cap"] = true,                                    -- 月伞
        ["moon_cap_cooked"] = true,                             -- 月伞
        ["shroomcake"] = true,                                  -- 蘑菇蛋糕
    }
    local function player_on_eat(inst, owner,food,mult)
        if mushroom_prefab[food.prefab] then
            owner.components.hutao_com_healing_grove:Clear()
            owner.components.hutao_com_dialogue:Say("healing_grove.destory_by_mushroom")
            return
        end
        if food.components.edible then
            local food_sanity = food.components.edible.sanityvalue
            if food_sanity > 0 then
                mult = mult or 1
                owner.components.sanity:DoDelta(0.5*food_sanity*mult)
            end
        end
    end
    --- 给外部调用.就不用重新封装食物列表了，
    local function on_eat_event_install(inst)
        inst:ListenForEvent("player_on_eat",function(inst,_table)
            local owner = _table and _table.owner or nil
            local food = _table and _table.food or nil
            local mult = _table and _table.mult or 1
            if owner and food then
                player_on_eat(inst,owner,food,mult)
            end
        end)
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
---装备、脱下
    local function onequip(inst, owner)
        -----------------------------------------------------
        --- 检查tag
            if not owner:HasTag("hutao") then
                return
            end
        -----------------------------------------------------
        --- 
        -----------------------------------------------------
        --- 计时器
            if inst._finiteuses_task then
                inst._finiteuses_task:Cancel()
            end
            inst._finiteuses_task = inst:DoPeriodicTask(1,function()
                inst.components.finiteuses:Use()
            end)
        -----------------------------------------------------
        --- 属性配置            
            inst:DoTaskInTime(0.1,function()
                if owner.components.combat then
                    owner.components.combat.externaldamagetakenmultipliers:SetModifier(inst,0.5)
                end
            end)
        -----------------------------------------------------
        --- 被攻击事件监听
            inst.__player_attacked_event = inst.__player_attacked_event or function()
                inst.___shell_timer = 0
                if owner.components.hutao_com_healing_grove:GetCurrent() > 0 then
                    local current_sanity = owner.components.sanity.current
                    owner.components.hutao_com_healing_grove:SanityDoDelta(-current_sanity*0.1)
                    local flg,actived = pcall(supercrack,inst)
                    if flg == actived then
                        owner.components.hutao_com_dialogue:Say("healing_grove.whip")
                    end
                end
            end            
            inst:ListenForEvent("attacked",inst.__player_attacked_event,owner)
        -----------------------------------------------------
        --- 启动计时器
            if inst._shell_task then
                inst._shell_task:Cancel()
            end
            inst._shell_task = inst:DoPeriodicTask(1,function()
                if owner.components.hutao_com_healing_grove:GetCurrent() > 0 then
                    inst.___shell_timer = 0
                    return
                end
                inst.___shell_timer = (inst.___shell_timer or 0) + 1
                if inst.___shell_timer >= SHELL_START_TIME then
                    activate_healing_grove(inst,owner)
                end
            end)
        -----------------------------------------------------
        --- max sanity update
            inst.__max_sanity_update_event = inst.__max_sanity_update_event or function(_,_table)
                local activing = _table and _table.activing
                local old_value = _table and _table.old or 0
                local new_value = _table and _table.new or 0
                if activing then
                    local current_shell = owner.components.hutao_com_healing_grove:GetCurrent()
                    owner.components.hutao_com_max_value_controller:AddTempExtraSanity(inst,current_shell*0.25)
                else
                    owner.components.hutao_com_max_value_controller:RemoveTempExtraSanity(inst)
                end
                if old_value > 0 and new_value == 0 then
                    owner.components.hutao_com_dialogue:Say("healing_grove.destory_by_empty")
                end                
            end
            inst:ListenForEvent("hutao_com_healing_grove_update",inst.__max_sanity_update_event,owner)
        -----------------------------------------------------
        --- eater event
            inst.__player_eater_event = inst.__player_eater_event or function(owner,_table)
                local food = _table and _table.food
                if food then
                    player_on_eat(inst,owner,food)
                end
            end
            inst:ListenForEvent("oneat",inst.__player_eater_event,owner)
        -----------------------------------------------------
        ---
            owner.AnimState:OverrideSymbol("arm_lower","hutao_equipment_black_hoodie_of_a_depressed_girl","arm_lower")
            owner.AnimState:OverrideSymbol("arm_upper","hutao_equipment_black_hoodie_of_a_depressed_girl","arm_upper")
            owner.AnimState:OverrideSymbol("torso","hutao_equipment_black_hoodie_of_a_depressed_girl","torso")
        -----------------------------------------------------
    end

    local function onunequip(inst, owner)
        -----------------------------------------------------
        --- 灯光
            if inst.light_fx then
                inst.light_fx:Remove()
                inst.light_fx = nil
            end
        -----------------------------------------------------
        --- 计时器
            if inst._finiteuses_task then
                inst._finiteuses_task:Cancel()
                inst._finiteuses_task = nil
            end
        -----------------------------------------------------
        --- 属性配置
            if owner.components.combat then
                owner.components.combat.externaldamagetakenmultipliers:RemoveModifier(inst)
            end
            if owner.components.hutao_com_healing_grove then
                owner.components.hutao_com_healing_grove:Clear()
            end
        -----------------------------------------------------
        --- 计时器清空
            inst.___shell_timer = 0
        -----------------------------------------------------
        --- 被攻击事件监听
            inst:RemoveEventCallback("attacked",inst.__player_attacked_event,owner)
            inst.__player_attacked_event = nil
        -----------------------------------------------------
        --- max sanity update
            inst:RemoveEventCallback("hutao_com_healing_grove_update",inst.__max_sanity_update_event,owner)
            inst.__max_sanity_update_event = nil
            if owner.components.hutao_com_max_value_controller then
                owner.components.hutao_com_max_value_controller:RemoveTempExtraSanity(inst)
            end
        -----------------------------------------------------
        --- 吃东西
            inst:RemoveEventCallback("oneat",inst.__player_eater_event,owner)
            inst.__player_eater_event = nil
        -----------------------------------------------------
        ---
            owner.AnimState:ClearOverrideSymbol("arm_lower")
            owner.AnimState:ClearOverrideSymbol("arm_upper")
            owner.AnimState:ClearOverrideSymbol("torso")
        -----------------------------------------------------
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 本体
    local function fn()
        local inst = CreateEntity()

        inst.entity:AddTransform()
        inst.entity:AddAnimState()
        inst.entity:AddSoundEmitter()
        inst.entity:AddNetwork()

        MakeInventoryPhysics(inst)

        inst.AnimState:SetBank("hutao_equipment_black_hoodie_of_a_depressed_girl")
        inst.AnimState:SetBuild("hutao_equipment_black_hoodie_of_a_depressed_girl")
        inst.AnimState:PlayAnimation("idle")
        local scale = 1.5
        inst.AnimState:SetScale(scale,scale,scale)
        inst:AddTag("hutao_equipment_black_hoodie_of_a_depressed_girl")


        MakeInventoryFloatable(inst)

        inst.entity:SetPristine()

        --------------------------------------------------------------------------
        if not TheWorld.ismastersim then
            return inst
        end
        -----------------------------------------------------
        --- 数据
            inst:AddComponent("hutao_data")
        -----------------------------------------------------
        --- 物品
            inst:AddComponent("inspectable")
            inst:AddComponent("inventoryitem")
            -- inst.components.inventoryitem:ChangeImageName("cane")
            inst.components.inventoryitem.imagename = "hutao_equipment_black_hoodie_of_a_depressed_girl"
            inst.components.inventoryitem.atlasname = "images/inventoryimages/hutao_equipment_black_hoodie_of_a_depressed_girl.xml"
        -----------------------------------------------------
        --- 装备
            inst:AddComponent("equippable")
            inst.components.equippable:SetOnEquip(onequip)
            inst.components.equippable:SetOnUnequip(onunequip)
            inst.components.equippable.equipslot = EQUIPSLOTS.BODY
            inst.components.equippable.restrictedtag = "hutao"
        -----------------------------------------------------
        --- 位面防御
            inst:AddComponent("planardefense")
	        inst.components.planardefense:SetBaseDefense(10)
        -----------------------------------------------------
        --- 耐久度
            inst:AddComponent("finiteuses")
            inst.components.finiteuses:SetMaxUses(MAX_FINITE_USES)
            inst.components.finiteuses:SetUses(MAX_FINITE_USES)
            inst.components.finiteuses:SetOnFinished(inst.Remove)
        -----------------------------------------------------
        --- 作祟
            MakeHauntableLaunch(inst)
            on_eat_event_install(inst)
        ---------------------------------------------------------
        return inst
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
return Prefab("hutao_equipment_black_hoodie_of_a_depressed_girl", fn, assets)
